class ApiService {
    constructor(role) {
        this.baseUrl = `http://localhost:8080/api/${role}`;
    }

    async login(accountId, password) {
        const response = await fetch(`${this.baseUrl}/login`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                accountId,
                password
            })
        });
        return await response.json();
    }

    async register(username, passWordF, passwordS, qualificationContent) {
        const response = await fetch(`${this.baseUrl}/register`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                username,
                passWordF,
                passwordS,
                qualificationContent
            })
        });
        return await response.json();
    }

    async changePassword(accountId, oldPassword, newPassword) {
        const response = await fetch(`${this.baseUrl}/changePassword`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                accountId,
                oldPassword,
                newPassword
            })
        });
        return await response.json();
    }

    async placeOrder(credentials) {
        const response = await fetch(`${this.baseUrl}/placeOrder`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(credentials)
        });
        return await response.json();
    }

    async pay(credentials) {
        const response = await fetch(`${this.baseUrl}/pay`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(credentials)
        });
        return await response.json();
    }

    async getAvailableProducts() {
        const response = await fetch(`${this.baseUrl}/getAvailableProducts`, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
            }
        });
        return await response.json();
    }
    //确认收货
    async confirmReceipt(credentials){
        const response = await fetch(`${this.baseUrl}/confirmReceipt`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(credentials)
        });
    }
// 获取已购买的商品
    async getPlacedProducts(buyerId) {
        const response = await fetch(`${this.baseUrl}/getPlacedProducts`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ buyerId })
        });
        return await response.json();
    }

    // 管理员获取所有用户
    async getAllUsers() {
        const response = await fetch(`${this.baseUrl}/getAllUsers`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            }
        });
        return await response.json();
    }

    // 管理员审核资质
    async auditQualification(accountId) {
        const response = await fetch(`${this.baseUrl}/auditQualification`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ accountId })
        });
        return await response.json();
    }
    
    // 提交物流订单信息
    async submitLogisticsOrder(orderData) {
        const response = await fetch(`${this.baseUrl}/postLogisticsInfo`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(orderData)
        });
        return await response.json();
    }
    //删除商品
    async deleteGoodsInfo (orderData) {
        const response = await fetch(`${this.baseUrl}/deleteGoodsInfo`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(orderData)
        });
        return await response.json();
    }

    //修改商品
    // 修改商品
async updateGoodsInfo(formData) {
    const response = await fetch(`${this.baseUrl}/updateGoodsInfo`, {
        method: 'POST',
        body: formData
        // 不要设置 Content-Type，让浏览器自动设置 multipart/form-data
    });
    return await response.json();
}


    // 获取卖家商品列表
    async showGoodsInfo(orderData) {
        const response = await fetch(`${this.baseUrl}/showGoodsInfo`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(orderData)
        });
        return await response.json();
    }

    //提交商品制造信息
    async postGoodsMadeInfo(goodsMadeInfo) {
        const response = await fetch(`${this.baseUrl}/postGoodsMadeInfo`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(goodsMadeInfo)
        });
        return await response.json();
    }
    
    // 获取卖家订单列表
    async getSellerOrders(credentials) {
        const response = await fetch(`${this.baseUrl}/showBuyerOrderInfo`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            // 发送请求体，包含sellerId
            body: JSON.stringify(credentials)
        });
        return await response.json();
    }
    
    // 发货功能
    async deliverGoods(credentials) {
        const response = await fetch(`${this.baseUrl}/deliverGoods`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(credentials)
        });
        return await response.json();
    }

    //退货
    async returnProduct(credentials) {
        const response = await fetch(`${this.baseUrl}/returnProduct`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(credentials)
        });
        return await response.json();
    }

    // 取消订单
    async cancelOrder(credentials) {
        const response = await fetch(`${this.baseUrl}/cancelOrder`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(credentials)
        });
        return await response.json();
    }

}

export default ApiService